{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "#### 1、完成nan和inf的使用，掌握数组中nan值去除，替换为对应列的平均值",
   "id": "37cdb2c3450accd4"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T11:11:04.177962Z",
     "start_time": "2025-01-08T11:11:03.909226Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#nan为缺失数据，inf为无穷大\n",
    "#nan ！= nan 为 ture  ，  inf ！= inf 为 false\n",
    "import numpy as np\n",
    "#nan 是not a number的缩写，表示非数字，在numpy中表示一个非数字值，可以用来表示缺失数据\n",
    "# 创建一个nan和inf #\n",
    "a = np.nan\n",
    "b = np.inf\n",
    "print(a, type(a))\n",
    "print(b, type(b))\n"
   ],
   "id": "290d75b8bb5356c5",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "nan <class 'float'>\n",
      "inf <class 'float'>\n"
     ]
    }
   ],
   "execution_count": 1
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T11:11:14.315083Z",
     "start_time": "2025-01-08T11:11:14.304237Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 并 且  np.nan != np.nan\t结果 是TRUE\n",
    "# 所以我们可以使用这两个结合使用判断nan的个数\n",
    "print(np.nan == np.nan)\n",
    "print(True == 1)\n",
    "print(False == 0)\n",
    "np.nan + 1  #nan和其他数运算的结果都是nan"
   ],
   "id": "dc758cde38221018",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n",
      "True\n",
      "True\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "nan"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 2
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T11:12:58.613289Z",
     "start_time": "2025-01-08T11:12:58.608436Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# --判断数组中为nan的个数\n",
    "t = np.arange(24, dtype=float).reshape(4, 6)\n",
    "\n",
    "# 将三行四列的数改成nan\n",
    "t[3, 4] = np.nan\n",
    "t[2, 4] = np.nan\n",
    "print(t)"
   ],
   "id": "d202419894c07dfb",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0.  1.  2.  3.  4.  5.]\n",
      " [ 6.  7.  8.  9. 10. 11.]\n",
      " [12. 13. 14. 15. nan 17.]\n",
      " [18. 19. 20. 21. nan 23.]]\n"
     ]
    }
   ],
   "execution_count": 8
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T11:11:58.822304Z",
     "start_time": "2025-01-08T11:11:58.815875Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 可以使用np.count_nonzero() 来判断非零的个数\n",
    "# print(np.count_nonzero(t))\n",
    "print(t != t)\n",
    "print('-' * 50)\n",
    "print(np.count_nonzero(t != t))  # 统计nan的个数"
   ],
   "id": "598bb2089adba2b3",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[False False False False False False]\n",
      " [False False False False False False]\n",
      " [False False False False  True False]\n",
      " [False False False False  True False]]\n",
      "--------------------------------------------------\n",
      "2\n"
     ]
    }
   ],
   "execution_count": 5
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T11:13:00.438050Z",
     "start_time": "2025-01-08T11:13:00.432066Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 将nan替换为0\n",
    "print(t)\n",
    "t[np.isnan(t)] = 0\n",
    "print(t)"
   ],
   "id": "d3eae97c13bafb8e",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0.  1.  2.  3.  4.  5.]\n",
      " [ 6.  7.  8.  9. 10. 11.]\n",
      " [12. 13. 14. 15. nan 17.]\n",
      " [18. 19. 20. 21. nan 23.]]\n",
      "[[ 0.  1.  2.  3.  4.  5.]\n",
      " [ 6.  7.  8.  9. 10. 11.]\n",
      " [12. 13. 14. 15.  0. 17.]\n",
      " [18. 19. 20. 21.  0. 23.]]\n"
     ]
    }
   ],
   "execution_count": 9
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T11:13:22.665012Z",
     "start_time": "2025-01-08T11:13:22.653871Z"
    }
   },
   "cell_type": "code",
   "source": "import numpy as np",
   "id": "ce7406a39606336b",
   "outputs": [],
   "execution_count": 10
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T11:14:23.702970Z",
     "start_time": "2025-01-08T11:14:23.699943Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# ----------练习： 处理数组中nan\n",
    "t = np.arange(24).reshape(4, 6).astype('float')\n",
    "print(id(t))\n",
    "# 将数组中的一部分替换nan\n",
    "t[1, 3:] = np.nan\n",
    "print(t)\n",
    "print('-------------------')\n",
    "print(t.shape)\n",
    "print(id(t))\n"
   ],
   "id": "1a21c9b820b4753e",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2802452794320\n",
      "[[ 0.  1.  2.  3.  4.  5.]\n",
      " [ 6.  7.  8. nan nan nan]\n",
      " [12. 13. 14. 15. 16. 17.]\n",
      " [18. 19. 20. 21. 22. 23.]]\n",
      "-------------------\n",
      "(4, 6)\n",
      "2802452794320\n"
     ]
    }
   ],
   "execution_count": 12
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T11:14:36.835165Z",
     "start_time": "2025-01-08T11:14:36.829048Z"
    }
   },
   "cell_type": "code",
   "source": [
    "t.shape[1]   #轴1个数\n",
    "#很长的一句代码，不理解，拆出来一部分来理解"
   ],
   "id": "ed7d1fc94ea4d52c",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 13
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T11:15:22.792132Z",
     "start_time": "2025-01-08T11:15:22.787528Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 遍历每一列，然后判断每一列是否有nan\n",
    "for i in range(t.shape[1]):\n",
    "    #获取当前列数据\n",
    "    temp_col = t[:, i]\n",
    "\n",
    "    # 判断当前列的数据中是否含有nan\n",
    "    nan_num = np.count_nonzero(temp_col != temp_col)\n",
    "    # 条件成立说明含有nan\n",
    "    if nan_num != 0:\n",
    "        # 将这一列不为nan的数据拿出来,并计算平均值\n",
    "        temp_col_not_nan = temp_col[temp_col == temp_col]\n",
    "        print(temp_col_not_nan)\n",
    "        # 将nan替换成这一列的平均值\n",
    "        temp_col[np.isnan(temp_col)] = np.mean(temp_col_not_nan)\n",
    "print('-'*50)\n",
    "print(t)\n",
    "print(id(t))"
   ],
   "id": "368dd4472de84f5e",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 3. 15. 21.]\n",
      "[ 4. 16. 22.]\n",
      "[ 5. 17. 23.]\n",
      "--------------------------------------------------\n",
      "[[ 0.  1.  2.  3.  4.  5.]\n",
      " [ 6.  7.  8. 13. 14. 15.]\n",
      " [12. 13. 14. 15. 16. 17.]\n",
      " [18. 19. 20. 21. 22. 23.]]\n",
      "2802452794320\n"
     ]
    }
   ],
   "execution_count": 14
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T11:38:23.697553Z",
     "start_time": "2025-01-08T11:38:23.692043Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(np.inf == np.inf)\n",
    "np.inf"
   ],
   "id": "672e3367843c5e1a",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "inf"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 15
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T11:38:30.090492Z",
     "start_time": "2025-01-08T11:38:30.086509Z"
    }
   },
   "cell_type": "code",
   "source": "np.inf >-np.inf      # 无穷大 比 无穷小 大",
   "id": "d2d99bbed36bd8b2",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 16
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T11:39:08.982510Z",
     "start_time": "2025-01-08T11:39:08.977927Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# np.nan和任何数据运算的结果都是nan\n",
    "t = np.arange(24).reshape(4, 6).astype('float')\n",
    "#\n",
    "# 将数组中的一部分替换nan\n",
    "t[1, 3:] = np.nan\n",
    "\n",
    "t1 = np.arange(24).reshape(4, 6).astype('float')\n",
    "t + t1"
   ],
   "id": "aefa1851c64b8f10",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.,  2.,  4.,  6.,  8., 10.],\n",
       "       [12., 14., 16., nan, nan, nan],\n",
       "       [24., 26., 28., 30., 32., 34.],\n",
       "       [36., 38., 40., 42., 44., 46.]])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 18
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T11:41:37.382206Z",
     "start_time": "2025-01-08T11:41:37.376412Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr = np.array([-1, 0])\n",
    "print(arr)\n",
    "print(arr[0] / arr[1])  #1除0就会得到inf，也就是无穷大"
   ],
   "id": "8b6c15c6cda2e600",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[-1  0]\n",
      "-inf\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\EAST\\AppData\\Local\\Temp\\ipykernel_9280\\2463273533.py:3: RuntimeWarning: divide by zero encountered in scalar divide\n",
      "  print(arr[0] / arr[1])  #1除0就会得到inf，也就是无穷大\n"
     ]
    }
   ],
   "execution_count": 22
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T11:40:56.781059Z",
     "start_time": "2025-01-08T11:40:56.776883Z"
    }
   },
   "cell_type": "code",
   "source": [
    "np.nan + np.inf\n",
    "# np.nan和任何数据运算的结果都是nan"
   ],
   "id": "52b27466611fab27",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "nan"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 19
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 转置和轴滚动",
   "id": "3bb734b77d318fa3"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T11:41:53.372001Z",
     "start_time": "2025-01-08T11:41:53.367639Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#对换数组的维度\n",
    "import numpy as np\n",
    "\n",
    "a = np.arange(12).reshape(3, 4)\n",
    "print('原数组：')\n",
    "print(a)\n",
    "print('\\n')\n",
    "\n",
    "print('对换数组：')      #二维数组的转置，也可以通过swapaxes和rollaxes完成\n",
    "print(np.transpose(a))\n",
    "print(a)\n",
    "\n",
    "# 与transpose一致\n",
    "a = np.arange(12).reshape(3, 4)\n",
    "\n",
    "print('原数组：')\n",
    "print(a)\n",
    "print('\\n')\n",
    "\n",
    "print('转置数组：')\n",
    "print(a.T)"
   ],
   "id": "753710b6dd1e73d2",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原数组：\n",
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]]\n",
      "\n",
      "\n",
      "对换数组：\n",
      "[[ 0  4  8]\n",
      " [ 1  5  9]\n",
      " [ 2  6 10]\n",
      " [ 3  7 11]]\n",
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]]\n",
      "原数组：\n",
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]]\n",
      "\n",
      "\n",
      "转置数组：\n",
      "[[ 0  4  8]\n",
      " [ 1  5  9]\n",
      " [ 2  6 10]\n",
      " [ 3  7 11]]\n"
     ]
    }
   ],
   "execution_count": 23
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T11:42:43.837367Z",
     "start_time": "2025-01-08T11:42:43.831367Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 函数用于交换数组的两个轴\n",
    "t1 = np.arange(24).reshape(4, 6)\n",
    "re1 = t1.swapaxes(1, 0)\n",
    "\n",
    "print(' 原 数 组 ：')\n",
    "print(t1)\n",
    "print('\\n')\n",
    "print(re1.shape)\n",
    "print('调用 swapaxes 函数后的数组：')\n",
    "print(re1)"
   ],
   "id": "d3f16a235c72330",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " 原 数 组 ：\n",
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "\n",
      "\n",
      "(6, 4)\n",
      "调用 swapaxes 函数后的数组：\n",
      "[[ 0  6 12 18]\n",
      " [ 1  7 13 19]\n",
      " [ 2  8 14 20]\n",
      " [ 3  9 15 21]\n",
      " [ 4 10 16 22]\n",
      " [ 5 11 17 23]]\n"
     ]
    }
   ],
   "execution_count": 24
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T11:42:53.580662Z",
     "start_time": "2025-01-08T11:42:53.577181Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#如果是3维及以上，称为轴交换\n",
    "t3 = np.arange(60).reshape(3, 4, 5)\n",
    "print(t3.shape)\n",
    "print('-' * 50)\n",
    "t3 = np.swapaxes(t3, 1, 2)\n",
    "print(t3.shape)\n",
    "# print(t3) 数据不用记住，不用观察"
   ],
   "id": "9a3c1739fe6fedcf",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(3, 4, 5)\n",
      "--------------------------------------------------\n",
      "(3, 5, 4)\n"
     ]
    }
   ],
   "execution_count": 25
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T11:43:05.817517Z",
     "start_time": "2025-01-08T11:43:05.814043Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 数组的轴滚动,swapaxes每次只能交换两个轴，没有rollaxis方便，默认情况下轴滚动最前面\n",
    "a = np.ones((3, 4, 5, 6))\n",
    "# np.rollaxis(a, 2).shape\n",
    "b = np.rollaxis(a, 3, 1)\n",
    "print(b.shape)"
   ],
   "id": "e44911124b144c28",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(3, 6, 4, 5)\n"
     ]
    }
   ],
   "execution_count": 26
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T11:43:59.221673Z",
     "start_time": "2025-01-08T11:43:59.214570Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(b.shape)\n",
    "c=np.rollaxis(b, 1,4) #轴会滚动，直到它位于此位置之前\n",
    "d=np.rollaxis(b, 1,3) #滚动到3之前，也就是2的位置\n",
    "print(c.shape)\n",
    "print(d.shape)"
   ],
   "id": "efe2eb20837918f1",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(3, 6, 4, 5)\n",
      "(3, 4, 5, 6)\n",
      "(3, 4, 6, 5)\n"
     ]
    }
   ],
   "execution_count": 29
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T11:44:22.007382Z",
     "start_time": "2025-01-08T11:44:22.003162Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#数据拷贝，copy()和赋值的区别\n",
    "b = np.array([[1, 2, 3], [1, 2, 3]])\n",
    "a = b.copy()\n",
    "a"
   ],
   "id": "3d71c871cd212f30",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [1, 2, 3]])"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 30
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T11:45:48.664729Z",
     "start_time": "2025-01-08T11:45:48.661455Z"
    }
   },
   "cell_type": "code",
   "source": [
    "b[0, 0] = 3\n",
    "print(b)\n",
    "a       #copy的是一个新的ndarray\n",
    "print(id(a))\n",
    "print(id(b))"
   ],
   "id": "2c49238147b40faf",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[3 2 3]\n",
      " [1 2 3]]\n",
      "2800740218608\n",
      "2800740215920\n"
     ]
    }
   ],
   "execution_count": 32
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T11:46:35.031979Z",
     "start_time": "2025-01-08T11:46:35.027244Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#随机数生成\n",
    "arr = np.random.rand(2, 3, 4) #np.random.rand（）生成数据为0-1之间的（）维数组\n",
    "print(arr)"
   ],
   "id": "a01c281d8a2f20d8",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[0.36234565 0.56346619 0.67292262 0.53350185]\n",
      "  [0.82344671 0.41979376 0.18707643 0.9561474 ]\n",
      "  [0.30004819 0.48032528 0.42415873 0.73191553]]\n",
      "\n",
      " [[0.67598682 0.26959413 0.93950936 0.46574258]\n",
      "  [0.15916389 0.88901448 0.33347497 0.3040562 ]\n",
      "  [0.22860497 0.64203326 0.21952803 0.35125587]]]\n"
     ]
    }
   ],
   "execution_count": 33
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T11:46:55.195585Z",
     "start_time": "2025-01-08T11:46:55.192112Z"
    }
   },
   "cell_type": "code",
   "source": [
    "names = 'zhangsan,lisi,wangwu,zhaoliu,sunqi'\n",
    "#写入文件\n",
    "with open('names.csv', 'w') as f:\n",
    "    f.write(names)\n",
    "    f.write('\\n')\n",
    "    f.write('12,23,34,45,56')"
   ],
   "id": "7e34758aa2e9eea",
   "outputs": [],
   "execution_count": 34
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
